<?php 
	global $CFG;

    require_once(dirname(dirname(dirname(__FILE__)))."/includes.php");
?>

/**
 * Wrapper for XMLHttpRequest used for MSG AJAX functionality.
 * Chris Denham
 * chris@iconographics.co.uk
 * Nov 2006
 */

var showXMLRequestError = true;

/**
 * Issues XMLHttpRequest method=GET and invokes the specified callback
 * with received XML document as the first parameter.
 * [Tip for future, if use for POST request, set header like:
 * req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 * (put it after open(), but before send())
 * ]
 */
function XMLRequest(url, callback, mode) {
    // branch for native XMLHttpRequest object (Mozilla etc..)
    var req = null;
	var asyncMode = true;
	if (mode == "sync") asyncMode = false;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        setupCallback(req, callback);
        req.open("GET", url, asyncMode);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            setupCallback(req, callback);
            req.open("GET", url, asyncMode);
            req.send();
        }
    }
    return req;
}

function XMLRequestPost(url, data, callback) {
    // branch for native XMLHttpRequest object (Mozilla etc..)
    var req = null;
	var asyncMode = true;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        setupCallback(req, callback);
        req.open("POST", url + "&" + data, asyncMode);
        req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        req.send(data);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            setupCallback(req, callback);
            req.open("POST", url, asyncMode);
            req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            req.send(data);
        }
    }
    return req;
}

/**
 * As above, but prepends the serviceBase.
 * Makes life easier when changing service name.
 */
var serviceBase = "<?php print($CFG->wwwroot);?>local/";
function XMLRequestQ(query, callback,  mode) {
    var url = serviceBase + query;
    return XMLRequest(url, callback, mode);
}

function XMLRequestPostQ(query, data, callback) {
    var url = serviceBase + query;
    return XMLRequestPost(url, data, callback);
}

function handleError(callback){
    //
    callback(null);
}

/**
 * This method of setting up a callback has a subtle, but IMPORTANT effect.
 * It has taken a while for me to resolve an issue with the typical way
 * a callback if defined for the onreadystatechange event. This is because
 * if you simply attach a 'static' function to this event, there is no easy
 * way to find out which request object the response belongs to. But here
 * I define a 'dynamic' function as the callback and this allows the parameters
 * of this function to be referred to by the 'dynamic' function.
 * The typical AJAX example simply uses a global variable, but this would
 * cause bugs in scenarios where multiple simultaneous requests may be pending.
 */
function setupCallback(req, callback) {
    req.onreadystatechange = function () {
        if (req.readyState == 4) {
            try {
                if (req.status == 200)
                {
                    callback(req.responseXML);
                } else {
                    handleError(callback);
                }
            } catch(Exception) {
                // In FireFox this occurs when page is reloaded as well as when
                // connection is broken. So, so just assume it's broken for now.
                handleError(callback);
            }
        }
    }
}

